//
//  StampSettingViewController.m
//  ZhangJiangJun
//
//  Created by FENGYL on 2017/1/13.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "StampSettingViewController.h"
#import "NetworkCommand.h"
#import "StampLockViewController.h"

#import <MBProgressHUD.h>

@interface StampSettingViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *authorizationStampButton;
@property (weak, nonatomic) IBOutlet UILabel *stampCount;
@property (weak, nonatomic) IBOutlet UILabel *validTime;
@property (weak, nonatomic) IBOutlet UIView *stampCountView;
@property (weak, nonatomic) IBOutlet UIView *validTimeView;

@property (strong, nonatomic) UIPickerView *stampCountPickerView;
@property (strong, nonatomic) UIPickerView *validTimePickerView;
@property (strong, nonatomic) NSMutableArray *countArray;
@property (strong, nonatomic) NSString *stampCountString;
@property (strong, nonatomic) NSString *validTimeString;

@end

@implementation StampSettingViewController

-(NSMutableArray *)countArray{
    
    if (!_countArray) {
        _countArray = [NSMutableArray arrayWithCapacity:999];
        for (int i = 1; i < 999; i++) {
            
            NSString * countString = [NSString stringWithFormat:@"%03d",i];
            
            [_countArray addObject:countString];
            
        }
    }
    
    return _countArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
}

-(void)setUpUI{
    
    //初始化
    self.stampCountString = @"1";
    self.validTimeString = @"1";
    
    //背景
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"zjj_j_d"]]];
    
    //标题
    self.title = NSLocalizedString(LOCALIZABLE_AUTHORIZATION_SETTING, nil);
    
    //设置阴影
//    [self setViewShadow:self.authorizationStampButton];
    
    //
//    self.authorizationStampButton.userInteractionEnabled = NO;
    
    
    //按钮文字
    [self.authorizationStampButton setTitle:NSLocalizedString(LOCALIZABLE_STAMP_AUTHORIZATION, nil) forState:UIControlStateNormal];
    
    //盖章数量view
    [self.stampCount setText:NSLocalizedString(LOCALIZABLE_STAMP_COUNT, nil)];
    CGFloat pickerHeight = ([UIScreen mainScreen].bounds.size.height - 196)/2;
    self.stampCountPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.stampCount.frame), [UIScreen mainScreen].bounds.size.width, pickerHeight)];
    [self.stampCountView addSubview:self.stampCountPickerView];
    self.stampCountPickerView.delegate = self;
    self.stampCountPickerView.dataSource = self;
    
    
    //有效时间view
    [self.validTime setText:NSLocalizedString(LOCALIZABLE_VALID_TIME, nil)];
    self.validTimePickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.validTime.frame), [UIScreen mainScreen].bounds.size.width, pickerHeight)];
    [self.validTimeView addSubview:self.validTimePickerView];
    self.validTimePickerView.delegate = self;
    self.validTimePickerView.dataSource = self;
    
}

#pragma mark 阴影
-(void)setViewShadow:(UIView *)sender{
    
    sender.layer.shadowColor = [UIColor blackColor].CGColor;
    
    sender.layer.shadowOffset = CGSizeMake(2, 2);
    
    sender.layer.shadowOpacity = 0.4;
    
    sender.layer.shadowRadius = 4;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark pickerView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;
    
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        
        return self.countArray.count;
        
    }
    
    return 1;
    
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 1) {
        
        if (pickerView == self.stampCountPickerView) {
            
            return @"次";
            
        }else{
            
            return @"分钟";
            
        }
        
    }
    
    NSString * countString = self.countArray[row];
    
    return countString;
    
}

- (IBAction)stampAuthorization:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(LOCALIZABLE_STAMP_AUTHORIZATION_ALARM_TITLE, nil) message:NSLocalizedString(LOCALIZABLE_STAMP_AUTHORIZATION_ALARM_CONTEXT, nil) preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(LOCALIZABLE_ALERT_CONFIRM, nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self touchStampAuthorization];
        
        
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(LOCALIZABLE_ALERT_CANCEL, nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    
    
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0) {
        
        if (pickerView == self.stampCountPickerView) {
            
            self.stampCountString = [NSString stringWithFormat:@"%ld",row + 1];
            
        }else{
            
            self.validTimeString = [NSString stringWithFormat:@"%ld",row + 1];
            
        }
        
    }
    
}

#pragma mark 授权盖章
-(void)touchStampAuthorization{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [hud.label setText:NSLocalizedString(LOCALIZABLE_LOADING, nil)];
    
    hud.mode = MBProgressHUDModeIndeterminate;
    
    hud.removeFromSuperViewOnHide = YES;
    
    [NetworkCommand stampIsUnlock:YES stampCount:self.stampCountString time:self.validTimeString success:^{
        
        [hud hideAnimated:YES];
        
        StampLockViewController * stampLockViewController = [self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count) - 2];
        
        [stampLockViewController buttonSelect];
        
        [self.navigationController popViewControllerAnimated:YES];
        
        
        
    } failure:^{
        
        [hud.label setText:NSLocalizedString(LOCALIZABLE_FAILED, nil)];
        
        hud.mode = MBProgressHUDModeCustomView;
        
        [hud hideAnimated:YES afterDelay:0.5];
        
        
    }];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
